home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / Jim's CDEFs v1.30 / demo Source ƒ / demoWind.c < prev    next >
Encoding:
Text File  |  1994-11-06  |  13.7 KB  |  461 lines  |  [TEXT/KAHL]

  1. // -----------------------------------------------------------------------------
  2. //    File        : demoWind.c
  3. //    Date        : October 14, 1994
  4. //    Author        : Jim Stout
  5. //    Purpose        : routines used for the demoCDEF Window
  6. //
  7. // -----------------------------------------------------------------------------
  8. #include "demoWind.h"
  9.  
  10. #include "jimsCDEF.h"
  11. #include "dialogAssist.h"
  12.  
  13. extern SysEnvRec        gSysEnv;
  14. extern short             gDemoType;
  15. extern ControlHandle     gPopCtl, gBoxCtl;
  16. extern ControlHandle    gProgBar1, gProgBar2, gProgBar3;
  17.  
  18. MenuHandle        dynMenu;
  19.  
  20. //=============================================================================
  21. //    create new controls - called from doNewWindow() in demoShell.c
  22. //=============================================================================
  23. void    doNewControls(WindowPtr theWindow)
  24. {
  25.     Rect            r;
  26.     short            ht;
  27.     
  28.     if(dynMenu) {
  29.         DeleteMenu(512);
  30.         DisposeMenu(dynMenu);
  31.     }
  32.     dynMenu = 0;
  33.     
  34. //-----------------------------------------------------------------------------
  35. // create the popup menu control
  36. //-----------------------------------------------------------------------------
  37.  
  38.     SetRect(&r, 10, 6, 220, 25);
  39.     
  40.     gPopCtl = NewControl(theWindow, &r, "\pShow:", true,
  41.                         popupTitleRightJust,    // right justified
  42.                         132,                    // menu id
  43.                         popupInsetFrame+45,        // ext varCode + title width
  44.                         16*popUp+ctl3D, 0L);
  45.     
  46. //-----------------------------------------------------------------------------
  47. // create the group box, inset 10 pixels from edge of window.
  48. //-----------------------------------------------------------------------------
  49.  
  50.     r = theWindow->portRect;
  51.     InsetRect(&r, 10, 10);
  52.     r.top+=14;                                // leave room for popup above
  53.     
  54.     ht = r.bottom-r.top;                    // calculate true size of box
  55.     r.bottom = r.top+16;                    // but this is the control rect
  56.     
  57.     gBoxCtl = NewControl(theWindow, &r, "\p", true,
  58.                         0,                    // control value
  59.                         0,                    // minimum value
  60.                         ht,                    // true height of control
  61.                         16*groupBox+insetBox, 0L);
  62. }
  63.  
  64. //=============================================================================
  65. // Get rid of controls in current window and create new ones
  66. //=============================================================================
  67.  
  68. void doSwapControls(WindowPtr theWindow)
  69. {
  70.     short            newVal;
  71.     ControlHandle    thisCtl, nextCtl;
  72.     
  73.     newVal = GetCtlValue(gPopCtl);                // popUp menu setting
  74.     
  75.     if(newVal != gDemoType) {                    // if it changed, 
  76.  
  77. //-----------------------------------------------------------------------------
  78. // delete the existing controls (not the popup & groupbox)
  79. //-----------------------------------------------------------------------------
  80.         
  81.         thisCtl = nextCtl = ((WindowPeek)theWindow)->controlList;
  82.         while(thisCtl) {
  83.             nextCtl = (**thisCtl).nextControl;
  84.             if(thisCtl != gPopCtl && thisCtl != gBoxCtl)
  85.                 DisposeControl(thisCtl);
  86.             thisCtl = nextCtl;
  87.         }
  88.         if(dynMenu) {
  89.             DeleteMenu(512);
  90.             DisposeMenu(dynMenu);
  91.             dynMenu = 0;
  92.         }
  93.  
  94.         ValidRect(&theWindow->portRect);            // no flicker needed as
  95.         gDemoType = newVal;                            // new controls draw
  96.         
  97. //-----------------------------------------------------------------------------
  98. // create some new controls
  99. //-----------------------------------------------------------------------------
  100.  
  101.         switch(gDemoType) {
  102.             case 1:
  103.                 make3DControls(theWindow);
  104.             break;
  105.             case 2:
  106.                 makePopupControls(theWindow);
  107.             break;
  108.             case 3:
  109.                 makeArrowControls(theWindow);
  110.             break;
  111.             case 4:
  112.                 makeDateControls(theWindow);
  113.             break;
  114.             case 5:
  115.                 makeSliderControls(theWindow);
  116.             break;
  117.             case 6:
  118.                 makeProgressControls(theWindow);
  119.             break;
  120.         }
  121.     }
  122. }
  123.  
  124. //=============================================================================
  125. // Handle a mouse click in a control
  126. //=============================================================================
  127. void doControlClick(ControlHandle theCtl, short thePart)
  128. {
  129.     switch(gDemoType) {
  130.         case 1:
  131.             SetCtlValue(theCtl, !GetCtlValue(theCtl));
  132.         break;
  133.     }
  134. }
  135.  
  136. //=============================================================================
  137. // Move the progress bar controls
  138. //=============================================================================
  139. void doProgress()
  140. {
  141.     short    val, max, min;
  142.     
  143. // bump the value of the progress bars
  144.  
  145.     val = GetCtlValue(gProgBar1);
  146.     max = GetCtlMax(gProgBar1);
  147.     min = GetCtlMin(gProgBar1);
  148.     
  149.     if(val < max)
  150.         SetCtlValue(gProgBar1, ++val);
  151.     else
  152.         SetCtlValue(gProgBar1, min);
  153.  
  154. // make the "barber poles" rotate
  155.  
  156.     SetCtlValue(gProgBar2, !GetCtlValue(gProgBar2));
  157.     SetCtlValue(gProgBar3, !GetCtlValue(gProgBar3));
  158. }
  159.  
  160. //=============================================================================
  161. // create a couple of 3D control samples
  162. //=============================================================================
  163. void make3DControls(WindowPtr theWindow)
  164. {
  165.     ControlHandle    hCtl;
  166.     Rect            r;
  167.     Str255            s,numStr;
  168.     short            inx;
  169.     long            num;
  170.     
  171. //-----------------------------------------------------------------------------
  172. // create a couple of check boxes
  173. //-----------------------------------------------------------------------------
  174.  
  175.     SetRect(&r, 30, 73, 130, 89);
  176.     
  177.     for(inx=1;inx<3;inx++) {
  178.         GetIndString(s, 129, 3);
  179.         num = inx;
  180.         NumToString(num, numStr);
  181.         pStrCat((char *)s, (char *)numStr);
  182.         hCtl = NewControl(theWindow, &r, s, true,
  183.                             0,                    // control value
  184.                             0,                    // minimum value
  185.                             1,                    // maximum value
  186.                             16*button3D+checkBoxProc, 0L);
  187.         OffsetRect(&r, 0, 26);
  188.     }
  189.     
  190. //-----------------------------------------------------------------------------
  191. // create a couple of radio buttons
  192. //-----------------------------------------------------------------------------
  193.  
  194.     SetRect(&r, 150, 73, 250, 89);
  195.     
  196.     for(inx=1;inx<3;inx++) {
  197.         GetIndString(s, 129, 2);
  198.         num = inx;
  199.         NumToString(num, numStr);
  200.         pStrCat((char *)s, (char *)numStr);
  201.         hCtl = NewControl(theWindow, &r, s, true,
  202.                             0,                    // control value
  203.                             0,                    // minimum value
  204.                             1,                    // maximum value
  205.                             16*button3D+radioButProc, 0L);
  206.         OffsetRect(&r, 0, 26);
  207.     }
  208.     
  209. //-----------------------------------------------------------------------------
  210. // create a 2 line push button
  211. //-----------------------------------------------------------------------------
  212.  
  213.     SetRect(&r, 280, 73, 340, 113);
  214.     
  215.     hCtl = NewControl(theWindow, &r, "\pPush\rButton", true,
  216.                         0,                    // control value
  217.                         0,                    // minimum value
  218.                         1,                    // maximum value
  219.                         16*button3D+pushButProc, 0L);
  220. }
  221.  
  222. //=============================================================================
  223. // create some popup controls
  224. //=============================================================================
  225. void makePopupControls(WindowPtr theWindow)
  226. {
  227.     ControlHandle    theCtl;
  228.     Rect            r;
  229.     
  230. //-----------------------------------------------------------------------------
  231. // create a popup controll for the large icon menu
  232. //-----------------------------------------------------------------------------
  233.  
  234.     SetRect(&r, 20, 40, 280, 94);
  235.  
  236.     theCtl = NewControl(theWindow, &r, "\pIcons:", true,
  237.                         popupTitleRightJust,    // right justified
  238.                         133,                    // menu id
  239.                         70,                        // title width
  240.                         16*popUp+ctl3D, 0L);
  241.                         
  242. //-----------------------------------------------------------------------------
  243. // create a menu dynamically and a popup control for it.
  244. //-----------------------------------------------------------------------------
  245.  
  246.     dynMenu = NewMenu(512, "\pDynamic");
  247.     if(dynMenu) {
  248.         AppendMenu(dynMenu, "\pThis menu was");
  249.         AppendMenu(dynMenu, "\pcreated dynamically,");
  250.         AppendMenu(dynMenu, "\pnot read from a ");
  251.         AppendMenu(dynMenu, "\presource.");
  252.         InsertMenu(dynMenu, -1);
  253.  
  254.         SetRect(&r, 20, 104, 280, 123);
  255.     
  256.         theCtl = NewControl(theWindow, &r, "\pDynamic:", true,
  257.                         popupTitleRightJust,    // right justified
  258.                         512,                    // menu id
  259.                         70,                        // title width
  260.                         16*popUp, 0L);
  261.     }
  262.     
  263. //-----------------------------------------------------------------------------
  264. // create a menu with colored & styled text items
  265. //-----------------------------------------------------------------------------
  266.     OffsetRect(&r, 0, 29);
  267.     theCtl = NewControl(theWindow, &r, "\pColored:", true,
  268.                     popupTitleRightJust,    // right justified
  269.                     134,                    // menu id
  270.                     70,                        // title width
  271.                     16*popUp, 0L);
  272.     
  273. //-----------------------------------------------------------------------------
  274. // create a "type in" style menu - draws symbol only
  275. //-----------------------------------------------------------------------------
  276.  
  277.     SetRect(&r, 290, 40, 314, 94);
  278.  
  279.     theCtl = NewControl(theWindow, &r, "\p", true,
  280.                         popupTitleRightJust,    // right justified
  281.                         303,                    // menu id
  282.                         popupSymbolOnly,        // title width
  283.                         16*popUp, 0L);
  284.                         
  285. //-----------------------------------------------------------------------------
  286. // create a "icon only" style menu - draws icons only
  287. //-----------------------------------------------------------------------------
  288.  
  289.     SetRect(&r, 290, 94, 330, 134);
  290.  
  291.     theCtl = NewControl(theWindow, &r, "\p", true,
  292.                         popupTitleRightJust,    // right justified
  293.                         304,                    // menu id
  294.                         popupIconOnly,            // title width
  295.                         16*popUp+popupFixedWidth, 0L);
  296.                         
  297. //-----------------------------------------------------------------------------
  298. // create a "popupCenterText" style menu - draws item text centered in ctl rect
  299. //-----------------------------------------------------------------------------
  300.  
  301.     SetRect(&r, 325, 40, 344, 94);
  302.  
  303.     theCtl = NewControl(theWindow, &r, "\p", true,
  304.                         popupTitleRightJust,    // right justified
  305.                         302,                    // menu id
  306.                         popupCenterText,        // title width
  307.                         16*popUp+useWFont, 0L);
  308. }
  309.  
  310. //=============================================================================
  311. // create a small arrow control and a large 3D arrow control - both vertical
  312. // and horizontal variations.
  313. //=============================================================================
  314. void makeArrowControls(WindowPtr theWindow)
  315. {
  316.     ControlHandle    theCtl;
  317.     Rect            r;
  318.     
  319.     SetRect(&r, 60, 66, 177, 84);
  320.     
  321.     theCtl = NewControl(theWindow, &r, "\pNumber 1:", true,
  322.                         0,                    // control value
  323.                         -100,                // minimum value
  324.                         100,                // maximum value
  325.                         16*spinner, 0L);
  326.                         
  327.     SetRect(&r, 60, 94, 180, 119);
  328.     
  329.     theCtl = NewControl(theWindow, &r, "\pNumber 2:", true,
  330.                         0,                    // control value
  331.                         -100,                // minimum value
  332.                         100,                // maximum value
  333.                         16*spinner+bigArrows+ctl3D, 
  334.                         0L);
  335.                         
  336.     SetRect(&r, 200, 66, 325, 84);
  337.     
  338.     theCtl = NewControl(theWindow, &r, "\pNumber 3:", true,
  339.                         0,                    // control value
  340.                         -100,                // minimum value
  341.                         100,                // maximum value
  342.                         16*spinner+horizArrows, 
  343.                         0L);
  344.                         
  345.     SetRect(&r, 200, 94, 332, 119);
  346.     
  347.     theCtl = NewControl(theWindow, &r, "\pNumber 4:", true,
  348.                         0,                    // control value
  349.                         -100,                // minimum value
  350.                         100,                // maximum value
  351.                         16*spinner+bigArrows+horizArrows, 
  352.                         0L);
  353. }
  354.  
  355. //=============================================================================
  356. // create a default date control - has both date & time
  357. //=============================================================================
  358. void makeDateControls(WindowPtr theWindow)
  359. {
  360.     ControlHandle    theCtl;
  361.     Rect            r;
  362.     
  363.     SetRect(&r, 42, 55, 300, 73);
  364.     
  365.     theCtl = NewControl(theWindow, &r, "\pDate & Time:", true,
  366.                         0,                    // ignored
  367.                         ctl3D,                // 1 for 3D
  368.                         0,                    // 1 for 24 hour time
  369.                         16*dateTime, 0L);
  370.                         
  371.     SetRect(&r, 90, 83, 300, 101);
  372.     
  373.     theCtl = NewControl(theWindow, &r, "\pDate:", true,
  374.                         0,                    // ignored
  375.                         0,                    // 1 for 3D
  376.                         0,                    // 1 for 24 hour time
  377.                         16*dateTime+dateOnly, 0L);
  378.                         
  379.     SetRect(&r, 90, 111, 300, 129);
  380.     
  381.     theCtl = NewControl(theWindow, &r, "\pTime:", true,
  382.                         0,                    // ignored
  383.                         0,                    // 1 for 3D
  384.                         only24,                // 1 for 24 hour time
  385.                         16*dateTime+timeOnly, 0L);
  386. }
  387.  
  388. //=============================================================================
  389. // create both vertical and horizontal 3D sliders
  390. //=============================================================================
  391. void makeSliderControls(WindowPtr theWindow)
  392. {
  393.     ControlHandle    theCtl;
  394.     Rect            r;
  395.     
  396.     SetRect(&r, 50, 43, 50+vWid, 43+7*vHt+21);
  397.     
  398.     theCtl = NewControl(theWindow, &r, "\p", true,
  399.                         0,                    // control value
  400.                         0,                    // minimum set by CDEF to 0
  401.                         0,                    // maximum set by CDEF to 12*ticks
  402.                         16*vSlider+ctl3D, 
  403.                         0L);                // 7 ticks - range is 2-20
  404.                         
  405.     OffsetRect(&r, 52, 0);
  406.     
  407.     theCtl = NewControl(theWindow, &r, "\p", true,
  408.                         0,                    // control value
  409.                         0,                    // minimum set by CDEF to 0
  410.                         0,                    // maximum set by CDEF to 12*ticks
  411.                         16*vSlider, 
  412.                         5L);                // 5 ticks - range is 2-20
  413.                         
  414.     SetRect(&r, 183, 63, 183+hWid, 63+hHt);
  415.     
  416.     theCtl = NewControl(theWindow, &r, "\p", true,
  417.                         0,                    // control value
  418.                         0,                    // minimum set by CDEF to 0
  419.                         0,                    // maximum set by CDEF to 100
  420.                         16*hSlider+ctl3D, 0L);
  421.                         
  422.     OffsetRect(&r, 0, hHt+10);
  423.     
  424.     theCtl = NewControl(theWindow, &r, "\p", true,
  425.                         0,                    // control value
  426.                         0,                    // minimum set by CDEF to 0
  427.                         0,                    // maximum set by CDEF to 100
  428.                         16*hSlider, 0L);
  429. }
  430.  
  431. //=============================================================================
  432. // create a "normal" and "barber pole" progress bar (both horizontal)
  433. //=============================================================================
  434. void makeProgressControls(WindowPtr theWindow)
  435. {
  436.     Rect            r;
  437.     
  438.     SetRect(&r, 40, 68, 280, 80);
  439.     
  440.     gProgBar1 = NewControl(theWindow, &r, "\p", true,
  441.                         10,                    // control value
  442.                         0,                    // minimum value
  443.                         240,                // maximum value
  444.                         16*progBar, 0L);
  445.     
  446.     OffsetRect(&r, 0, 40);
  447.     
  448.     gProgBar2 = NewControl(theWindow, &r, "\p", true,
  449.                         1,                    // control value
  450.                         0,                    // minimum value
  451.                         1,                    // maximum value
  452.                         16*progBar+barberPole, 0L);
  453.                         
  454.     SetRect(&r, 315, 47, 331, 145);
  455.     
  456.     gProgBar3 = NewControl(theWindow, &r, "\p", true,
  457.                         1,                    // control value
  458.                         0,                    // minimum value
  459.                         1,                    // maximum value
  460.                         16*progBar+vertBar+roundBar+barberPole, 0L);
  461. }